package com.lollipop.rabbitmqspringboot.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Lollipop
 * @Date: 2021-10-18 20:25
 * @Description: 发布确认配置
 */
@Configuration
public class ConfirmConfig {
    /**
     * 交换机
     */
    public static final String CONFIRM_EXCHANGE_NAME = "confirm_exchange";
    /**
     * 队列
     */
    public static final String CONFIRM_QUEUE_NAME = "confirm_queue";
    /**
     * routing key
     */
    public static final String CONFIRM_ROUTING_KEY = "confirm_routing_key";

    /**
     * 备份交换机
     */
    public static final String BACKUP_EXCHANGE_NAME = "backup_confirm_exchange";
    /**
     * 备份队列
     */
    public static final String BACKUP_QUEUE_NAME = "backup_confirm_queue";
    /**
     * 告警队列
     */
    public static final String WARN_QUEUE_NAME = "warn_confirm_queue";

    /**
     * 声明交换机
     *
     * @return
     */
    @Bean
    public DirectExchange confirmExchange() {
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE_NAME).durable(true).withArgument("alternate-exchange", BACKUP_EXCHANGE_NAME).build();
    }

    /**
     * 声明队列
     *
     * @return
     */
    @Bean
    public Queue confirmQueue() {
        return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();
    }

    /**
     * 将以上声明的队列和交换机绑定
     *
     * @param confirmQueue
     * @param confirmExchange
     * @return
     */
    @Bean
    public Binding queueBindingConfirmExchange(Queue confirmQueue, DirectExchange confirmExchange) {
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_ROUTING_KEY);
    }


    /**
     * 声明备份交换机
     *
     * @return
     */
    @Bean
    public FanoutExchange backupConfirmExchange() {
        return new FanoutExchange(BACKUP_EXCHANGE_NAME);
    }

    /**
     * 声明备份队列
     *
     * @return
     */
    @Bean
    public Queue backupConfirmQueue() {
        return QueueBuilder.durable(BACKUP_QUEUE_NAME).build();
    }

    /**
     * 声明告警队列
     *
     * @return
     */
    @Bean
    public Queue warnConfirmQueue() {
        return QueueBuilder.durable(WARN_QUEUE_NAME).build();
    }

    /**
     * 将以上声明的备份队列和备份交换机绑定
     *
     * @param backupConfirmQueue
     * @param backupConfirmExchange
     * @return
     */
    @Bean
    public Binding backupQueueBindingConfirmExchange(Queue backupConfirmQueue, FanoutExchange backupConfirmExchange) {
        return BindingBuilder.bind(backupConfirmQueue).to(backupConfirmExchange);
    }

    /**
     * 将以上声明的告警队列和备份交换机绑定
     *
     * @param warnConfirmQueue
     * @param backupConfirmExchange
     * @return
     */
    @Bean
    public Binding warnQueueBindingConfirmExchange(Queue warnConfirmQueue, FanoutExchange backupConfirmExchange) {
        return BindingBuilder.bind(warnConfirmQueue).to(backupConfirmExchange);
    }
}
